package com.yunchang.springboot.scheduled.mwsfinance;

import com.yunchang.springboot.base.BaseService;
import com.yunchang.springboot.mapper.mwsfinance.FLongTermStorageFeesMapper;
import com.yunchang.springboot.mapper.mwsreport.MwsLongTermStorageFeeChargesMapper;
import com.yunchang.springboot.models.mwsfinance.FLongTermStorageFees;
import com.yunchang.springboot.models.mwsreportbean.MwsLongTermStorageFeeCharges;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 长期仓储费
 */
@Component
@Configuration
@Service
@Slf4j
@EnableScheduling
public class FLongTermStorageFeesScheduled extends BaseService {

    private static final String DEBIT_SUBJECT_ID = "660121";

    private static final String CREDIT_SUBJECT_ID = "220201";

    static  Integer  LIMIT_VALUE = 10000;

    static String DOCUMENTS_ID = "CQCCFYD";

    /**
     * 长期仓储费
     */
    private MwsLongTermStorageFeeChargesMapper mwsLongTermStorageFeeChargesMapper;

    @Autowired
    public void setMwsLongTermStorageFeeChargesMapper(MwsLongTermStorageFeeChargesMapper mwsLongTermStorageFeeChargesMapper) {
        this.mwsLongTermStorageFeeChargesMapper = mwsLongTermStorageFeeChargesMapper;
    }

    /**
     * 财务长期仓储费
     */
    private FLongTermStorageFeesMapper fLongTermStorageFeesMapper;

    @Autowired
    public void setFLongTermStorageFeesMapper(FLongTermStorageFeesMapper fLongTermStorageFeesMapper) {
        this.fLongTermStorageFeesMapper = fLongTermStorageFeesMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void getFLongTermStorageFeesDate() {
        List<MwsLongTermStorageFeeCharges> mwsLongTermStorageFeeChargesList = mwsLongTermStorageFeeChargesMapper.selectByFinanceStatus(1,null);
        Integer mwsLongTermStorageFeeChargesListSize = mwsLongTermStorageFeeChargesList.size();
        int forNum = 1;
        if(mwsLongTermStorageFeeChargesListSize>LIMIT_VALUE){
            forNum += mwsLongTermStorageFeeChargesListSize/LIMIT_VALUE;
            for (;forNum>0;forNum--){
                log.info("长期仓储费数据剩余几次处理："+forNum);
                mwsLongTermStorageFeeChargesList = mwsLongTermStorageFeeChargesMapper.selectByFinanceStatus(1,LIMIT_VALUE);
                mwsLongTermStorageFeeChargesDispose(mwsLongTermStorageFeeChargesList);
            }
        }else {
            mwsLongTermStorageFeeChargesDispose(mwsLongTermStorageFeeChargesList);
        }
    }

    public void mwsLongTermStorageFeeChargesDispose(List<MwsLongTermStorageFeeCharges> mwsLongTermStorageFeeChargesList){
        if (null != mwsLongTermStorageFeeChargesList && !mwsLongTermStorageFeeChargesList.isEmpty()) {
            List<Object> FLongTermStorageFeesList = new ArrayList<>(mwsLongTermStorageFeeChargesList.size());
            for (MwsLongTermStorageFeeCharges mwsLongTermStorageFeeCharges : mwsLongTermStorageFeeChargesList) {
                FLongTermStorageFees fLongTermStorageFees = new FLongTermStorageFees();
                fLongTermStorageFees.setApprovalStatus(1);
                fLongTermStorageFees.setDocumentsId(DOCUMENTS_ID + UUIDUtil.getUUID());
                fLongTermStorageFees.setSellerId(mwsLongTermStorageFeeCharges.getSellerId());
                fLongTermStorageFees.setCountry(mwsLongTermStorageFeeCharges.getCountry());
                fLongTermStorageFees.setDate(mwsLongTermStorageFeeCharges.getSnapshotDate().substring(0, 10));
                fLongTermStorageFees.setSku(mwsLongTermStorageFeeCharges.getSku());
                fLongTermStorageFees.setFnsku(mwsLongTermStorageFeeCharges.getFnsku());
                fLongTermStorageFees.setAsin(mwsLongTermStorageFeeCharges.getAsin());
                fLongTermStorageFees.setAmount(mwsLongTermStorageFeeCharges.getMoLongTermsStorageFee6().add(mwsLongTermStorageFeeCharges.getMoLongTermsStorageFee12()).setScale(4, BigDecimal.ROUND_HALF_UP));
                fLongTermStorageFees.setCurrency(mwsLongTermStorageFeeCharges.getCurrency());
                fLongTermStorageFees.setDebitSubjectId(DEBIT_SUBJECT_ID);
                fLongTermStorageFees.setCreditSubjectId(CREDIT_SUBJECT_ID);
                fLongTermStorageFees.setApprovalStatus(1);
                fLongTermStorageFees.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fLongTermStorageFees.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                mwsLongTermStorageFeeCharges.setFinanceStatus(2);
                FLongTermStorageFeesList.add(fLongTermStorageFees);
            }
            try {
                baseInsertList(FLongTermStorageFeesList);
                log.info("已把全部数据插入完成，待更新状态");
                mwsLongTermStorageFeeChargesMapper.updateFinanceStatusByMwsLongTermStorageFeeChargesList(mwsLongTermStorageFeeChargesList);
                log.info("状态已更新，齐活了");
            } catch (Exception e) {
                log.error("插入数据是出现错误，长期仓储费" + e);
                throw new RuntimeException("插入数据是出现错误，长期仓储费");
            }
        } else {
            log.info("未查询到对应的长期仓储费数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }
}
